package com.smh.aiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.smh.aiagent.constant.FileConstant;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * @author shiminghui
 * @date 2025/6/4 19:54
 * @description: 文件操作工具类
 */
public class FileOperationTool {

    private static final String FILE_DIR = FileConstant.FILE_DIR + "/" + "file";

    @Tool(description = "Read the contents of the specified file")
    public String readFileContent(@ToolParam(description = "Specify the file name of the file") String fileName,
                                  ToolContext toolContext) {

        // 拼装文件路径
        String filePath = FILE_DIR + "/" + fileName;

        // 使用糊涂工具包读取文件内容
        try {
            String fileContent = FileUtil.readUtf8String(filePath);
            return fileContent;
        } catch (Exception e) {
            return "Failed to read the file : " + e.getMessage();
        }
    }

    @Tool(description = "Write the specified content to the specified file")
    public String writeFileContent(@ToolParam(description = "Specify the file name of the file") String fileName,
                                   @ToolParam(description = "Specify the content to be written to the file") String content,
                                   ToolContext toolContext) {

        // 拼装文件路径
        String filePath = FILE_DIR + "/" + fileName;
        try {
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully, file path is " + filePath;
        } catch (Exception e) {
            return "Failed to write the file : " + e.getMessage();
        }

    }

}
